const validate = RxFormValidator.validator({
    rxElemValidators: [
        RxElemValidator.notBlank('name', '[域名类型] 不能为空'),
        RxElemValidator.limitLen('name', '[域名类型] 长度最大为 50 位', 50),
    ],
})

class SysDomainCategory {
    constructor({
                    id = '',
                    name = '',
                    delFlag = '0',
                } = {}) {
        this.id = id
        this.name = name
        this.delFlag = delFlag
    }
}

const app = new BasicTableVue({
    data() {
        return {
            api: sysDomainCategoryApi,
            formData: new SysDomainCategory(),
            dialogShow: false,
            dtitle: '',
            formLabelWidth: '70px',
            columns: [
                new TableColumn({width: 30, type: 'selection'}),
                new TableColumn({prop: 'name', title: '域名类型名称'}),
                new TableColumn({prop: 'operating', title: '操作', customComponent: true}),
            ],
        }
    },
    methods: {
        createForm() {
            return new SysDomainCategoryQuery()
        },
        addInfo() {
            this.dtitle = '新增'
            this.dialogShow = true
            this.formData = {}

        },
        updateInfo(row) {
            this.dtitle = '修改'
            this.formData = _.cloneDeep(row)
            this.dialogShow = true
        },
        async downloadTemplate() {
            await sysDomainCategoryApi.downloadTemplate('域名类型导入模板.xlsx')
        },
        async updateOrAddData() {
            if (!(await validate(this.formData))) {
                return
            }
            const res = await sysDomainCategoryApi.updateOrAdd(this.formData)
            if (!res || res.data !== true) {
                await rxPrompt.dangerMsg(this.dtitle + '失败')
                return
            }
            this.dialogShow = false
            await Promise.all([
                await rxPrompt.successMsg(this.dtitle + '成功'),
                await this.searchPage(),
            ])
        },
        async del({id}) {
            if (rx.stringValidator.isEmpty(id)) {
                await rxPrompt.dangerMsg('操作失败')
                return
            }
            const res = await rxPrompt.confirm('你确认要删除该记录吗?？', ['确认', '取消'])
            if (!res) {
                return
            }
            const result = await this.api.deleteById(id)
            if (result) {
                await Promise.all([
                    rxPrompt.successMsg('删除成功'),
                    this.searchPage(),
                ])
            }
        },
        async init() {

        },

    },
    computed: {
        fieldFormatter() {
            return fieldFormatter
        },
    },

})
